//
//  YuXiDB.m
//  DialectShare
//
//  Created by timmy zhong on 3/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "YuXiDB.h"
#import "FMDatabase.h"
#import "PathHelper.h"
#import "Const.h"
#import "YuXi.h"

@implementation YuXiDB

- (id)init
{
    self = [super init];
    if (self) {
        FMDatabase *db = [FMDatabase databaseWithPath:[PathHelper dbPath]];
        if ([db open]) {
            NSString* createTableStatement = [NSString stringWithFormat:
                                              @"""CREATE TABLE IF NOT EXISTS %@"""
                                              """(%@ INTEGER PRIMARY KEY, %@ INTEGER, %@ TEXT)""", YuXiTableName, YuXiId, YuXiListen, YuXiName];
            BOOL su = [db executeUpdate:createTableStatement]; 
            if (!su) {
                NSLog(@"fail to create table  %@  error  %@", YuXiTableName, [db lastErrorMessage]);
            }
            [db close];
        }
    }
    
    return self;
}

- (void)addYuXis:(NSArray *)yuxis
{
    FMDatabase *db = [FMDatabase databaseWithPath:[PathHelper dbPath]];
    if (![db open]) {
        NSLog(@"addYuXis: fail to open DB error  %@", [db lastErrorMessage]);
        return;
    }
    
    [db beginTransaction];
    for (YuXi *yuxi in yuxis) {
        
        NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@ (%@, %@, %@) values (?, ?, ?)", YuXiTableName , YuXiId, YuXiListen, YuXiName];
        [db executeUpdate:sql,
         [NSNumber numberWithInt:yuxi.yuxiId],
         [NSNumber numberWithInt:yuxi.listen],
         yuxi.name];
    }
    
    BOOL su = [db commit];
    if (!su) {
        NSLog(@"fail to add yuxis error %@", [db lastErrorMessage]);
    }
    [db close];

}

- (NSArray *)yuxis
{
    return [self condition:@"1" start:0 count:1000];
}

- (NSArray *)condition:(NSString *)condition start:(NSInteger)start count:(NSInteger)count
{
    NSString *orderBy = [NSString stringWithFormat:@"%@ desc", YuXiListen];
    return [self condition:condition orderBy:orderBy start:start count:count];
}

- (NSArray *)condition:(NSString *)condition orderBy:(NSString *)orderBy start:(NSInteger)start count:(NSInteger)count
{
    FMDatabase *db = [FMDatabase databaseWithPath:[PathHelper dbPath]];
    if (![db open]) {
        NSLog(@"getYuXis: fail to open DB error  %@", [db lastErrorMessage]);
        return nil;
    }
    
    NSString *sql = [NSString stringWithFormat:@"select * from %@ where %@ order by %@ limit %d, %d", YuXiTableName, condition, orderBy, start, count];
    FMResultSet *rs = [db executeQuery:sql];
    NSMutableArray *yuxis = [[NSMutableArray alloc] initWithCapacity:20];
    while ([rs next]) {
        YuXi *yuxi = [[YuXi alloc] init];
        yuxi.yuxiId = [rs intForColumn:YuXiId];
        yuxi.listen = [rs intForColumn:YuXiListen];
        yuxi.name = [rs stringForColumn:YuXiName];
        [yuxis addObject:yuxi];
        [yuxi release];
    }
    [db close];
    return [yuxis autorelease];

}

@end
